package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.*;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.domain.*;
import cn.uexpo.lhz.plus.R;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 *摄像头位置
 *
 * @author
 * @since
 */
@RestController
@RequestMapping("/cert/activity")
public class ActivityController extends BaseController<Many> {
	@Autowired
	private ManyBiz manyBiz;
	@Autowired
	private EventBiz eventBiz;
	@Autowired
	private EquipmentBiz equipmentBiz;
	@Autowired
	private HallBiz hallBiz;
	@Autowired
	private ActivityMapBiz activityMapBiz;
	@Autowired
	private CamerastatusBiz camerastatusBiz;

	/**
	 * vue下拉框项目 场馆 数据展示
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dataHallList", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> dataHallList() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<JSONObject> hallList = new ArrayList<JSONObject>();
		HallExample hallExample = new HallExample();
		List<Hall> halls = hallBiz.selectByExample(hallExample);
		for (Hall hall : halls) {
			JSONObject obj = new JSONObject();
			obj.put("hallname", hall.getName());
			hallList.add(obj);
		}
		map.put("halls", hallList);
		return map;
	}

	/**
	 * vue下拉框项目 展会 数据展示
	 *
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value = "/dataEventList")
	public R dataEventList()  {
		EventExample eventExample = new EventExample();
		List<String> collect = eventBiz.selectByExample(eventExample)
	       .stream()
				.map(e -> e.getName())
				.collect(Collectors.toList());
		return R.ok().put("events",collect);
	}

	/**
	 * vue下拉框项目  摄像头数据 图片 标点展示
	 *
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value = "/dataCameraList")
	public Map<String, Object> dataCameraList(@RequestParam Map<String, Object> params)  {


		Map<String, Object> map = new HashMap<String, Object>();
		String eventName = (String) params.get("extstr1");
		String base64Img = null;
		String location = null;
		List<JSONObject> cameraList = new ArrayList<JSONObject>();
		EventExample eventExample = new EventExample();
		EventExample.Criteria eventCiteria = eventExample.createCriteria();
		eventCiteria.andNameEqualTo(eventName);
		List<Event> events = eventBiz.selectByExample(eventExample);
		if(null!=events&&events.size()>0) {
			Event event = events.get(0);
			int eventId = event.getId();
			ManyExample manyExample = new ManyExample();
			ManyExample.Criteria manyCiteria = manyExample.createCriteria();
			manyCiteria.andXxidEqualTo(eventId);
			manyCiteria.andTagEqualTo("event_equipment");
			List<Many> manys = manyBiz.selectByExample(manyExample);
			if(null!=manys&&manys.size()>0) {
				for(Many many:manys) {
					JSONObject obj = new JSONObject();
					int equipmentId = many.getYyid();
					EquipmentExample equipmentExample = new EquipmentExample();
					EquipmentExample.Criteria equipmentCiteria = equipmentExample.createCriteria();
					equipmentCiteria.andIdEqualTo(equipmentId);
					List<Equipment> equipments = equipmentBiz.selectByExample(equipmentExample);
					if(null!=equipments&&equipments.size()>0) {
						obj.put("cameraname", equipments.get(0).getCameraname());

						String equipmentNumber = equipments.get(0).getExstr1();

						String state = null;
						if(null!=equipmentNumber&&!"".equals(equipmentNumber)) {
							CamerastatusExample camerastatusExample = new CamerastatusExample();
							CamerastatusExample.Criteria camerastatusCiteria = camerastatusExample.createCriteria();
							camerastatusCiteria.andExtstr2EqualTo(equipmentNumber);
							List<Camerastatus> camerastatus = camerastatusBiz.selectByExample(camerastatusExample);
							if(null!=camerastatus&&camerastatus.size()>0) {
								state = camerastatus.get(camerastatus.size()-1).getStatus();
							}
						}

						obj.put("state", state);
						cameraList.add(obj);
					}


				}

			}

			//查询数据
			ActivityMapExample example = new ActivityMapExample();
			ActivityMapExample.Criteria citeria = example.createCriteria();
			citeria.andHallidEqualTo(eventId);
			List<ActivityMap> activitys = activityMapBiz.selectByExampleWithBLOBs(example);
			ActivityMap activity = new ActivityMap();
			if(null!=activitys&&activitys.size()>0) {
				byte[] base64 = activitys.get(0).getImage();
				if(null!=base64&&!"".equals(base64)) {
					base64Img = Base64.encodeBase64String(base64).replace("\r\n", "");
				}

				location = activitys.get(0).getExtstr1();
			}
		}


		map.put("cameras", cameraList);
		map.put("base64Img", base64Img);
		map.put("location", location);
		return map;
	}

	/**
	 * 照片上传将图片存入数据库
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveImg", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> saveImg(@RequestBody ActivityMap activityMap)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String code = "500";
		String msg = "失败";
		int no = 0;
		/**
		 * 截取存储前端已转Base64字码的图片
		 */
		String img = activityMap.getExtstr2();
		String eventName = activityMap.getExtstr1();

		EventExample eventExample = new EventExample();
		EventExample.Criteria eventCiteria = eventExample.createCriteria();
		eventCiteria.andNameEqualTo(eventName);
		List<Event> events = eventBiz.selectByExample(eventExample);
		if(null!=events&&events.size()>0) {
			int eventId = events.get(0).getId();

			Date date = new Date();

			ActivityMapExample example = new ActivityMapExample();
			ActivityMapExample.Criteria citeria = example.createCriteria();
			citeria.andHallidEqualTo(eventId);
			List<ActivityMap> activitys = activityMapBiz.selectByExample(example);
			ActivityMap activity = new ActivityMap();
			if(null!=activitys&&activitys.size()>0) {
				activity = activitys.get(0);
				activity.setImage(Base64.decodeBase64(img));
				activity.setCreatetime(date);
				no = activityMapBiz.updateByPrimaryKeySelective(activity);
			}else {
				activity.setImage(Base64.decodeBase64(img));
				activity.setCreatetime(date);
				activity.setHallid(eventId);
				no = activityMapBiz.insertSelective(activity);
			}
		}

		if(1==no) {
			code = "200";
			msg = "成功";
		}

		map.put("code", code);
		map.put("msg", msg);
		return map;
	}

	/**
	 *  将标点数据存入数据库
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/savePoint", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> savePoint(@RequestBody ActivityMap activityMap)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String code = "500";
		String msg = "失败";
		int no = 0;
		/**
		 * 截取存储前端已转Base64字码的图片
		 */
		String extstr1 = activityMap.getExtstr2();//标点数据
		String eventName = activityMap.getExtstr1();//展会名称

		EventExample eventExample = new EventExample();
		EventExample.Criteria eventCiteria = eventExample.createCriteria();
		eventCiteria.andNameEqualTo(eventName);
		List<Event> events = eventBiz.selectByExample(eventExample);
		if(null!=events&&events.size()>0) {
			int eventId = events.get(0).getId();

			Date date = new Date();

			ActivityMapExample example = new ActivityMapExample();
			ActivityMapExample.Criteria citeria = example.createCriteria();
			citeria.andHallidEqualTo(eventId);
			List<ActivityMap> activitys = activityMapBiz.selectByExample(example);
			ActivityMap activity = new ActivityMap();
			if(null!=activitys&&activitys.size()>0) {
				activity = activitys.get(0);
				activity.setExtstr1(extstr1);
				activity.setUpdatetime(date);
				no = activityMapBiz.updateByPrimaryKeySelective(activity);
			} else {
				activity.setExtstr1(extstr1);
				activity.setUpdatetime(date);
				activity.setHallid(eventId);
				no = activityMapBiz.insertSelective(activity);
			}
		}

		if(1==no) {
			code = "200";
			msg = "成功";
		}

		map.put("code", code);
		map.put("msg", msg);
		return map;
	}


}
